#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using XNADota.Input;
using Microsoft.Xna.Framework.Input;
#endregion

namespace JiggleGame.PhysicObjects
{
    class SphereController : Controller
    {
        private Body _body;
        private Game _game;

        public SphereController()
        {
        }

        public void Initialise(Body body, Game game)
        {
            _body = body;
            _game = game;
        }

        public override void UpdateController(float dt)
        {
            if (_body == null)
                return;

            InputState inputState = XNADota.XNADota.Instance.InputManager.InputSate;

            Vector3 position = _body.Position;

            Vector3 force = Vector3.Zero;

            if (inputState.IsKeyDown(Keys.W))
                force = new Vector3(1, 0, 0);
            if (inputState.IsKeyDown(Keys.S))
                force = new Vector3(-1, 0, 0);
            if (inputState.IsKeyDown(Keys.A))
                force = new Vector3(0, 0, -1);
            if (inputState.IsKeyDown(Keys.D))
                force = new Vector3(0, 0, 1);

            _body.AddBodyForce(force * dt * 100000 * _body.Mass);
            _body.AddBodyTorque(force * dt * 100000 * _body.Mass);
        }
    }

    public class SphereObject : PhysicObject
    {

        public SphereObject(Game game, Model model,float radius, Matrix orientation, Vector3 position)
            : base(game, model)
        {
            body = new Body();
            collision = new CollisionSkin(body);
            collision.AddPrimitive(new Sphere(Vector3.Zero * 5.0f,radius), new MaterialProperties(0.5f,0.7f,0.6f));
            body.CollisionSkin = this.collision;
            Vector3 com = SetMass(10.0f);
            body.MoveTo(position + com, orientation);
           // collision.ApplyLocalTransform(new Transform(-com, Matrix.Identity));
            body.EnableBody();
            this.scale = Vector3.One * radius;

            SphereController controller = new SphereController();
            controller.Initialise(body, game);
            controller.EnableController();
        }

        public override void ApplyEffects(BasicEffect effect)
        {
            effect.DiffuseColor = color;
        }

        //public void ProcessInput(GameTime gameTime, InputState inputState)
        //{
        //    float value = 100;
        //    if (inputState.IsKeyDown(Keys.W))
        //        body.AddBodyForce((new Vector3(1, 0, 0) * value));
        //    if (inputState.IsKeyDown(Keys.S))
        //        body.AddBodyForce(new Vector3(-1, 0, 0) * value);
        //    if (inputState.IsKeyDown(Keys.A))
        //        body.AddBodyForce(new Vector3(0, 0, -1) * value);
        //    if (inputState.IsKeyDown(Keys.D))
        //        body.AddBodyForce(new Vector3(0, 0, 1) * value);
        //}

    }
}
